package com.system.blogUser.common.controller;

import com.cnc.common.lang.page.PageBean;
import com.cnc.common.web.ezPage.EzPage;
import com.cnc.common.web.ezPage.EzPageParam;
import com.cnc.common.web.ezPage.PageUtils;
import com.system.blogAdmin.auth.dto.UserDto;
import com.system.blogAdmin.auth.entity.AuthResource;
import com.system.blogAdmin.auth.service.AuthResourceService;
import com.system.blogAdmin.auth.service.UserService;
import com.system.blogAdmin.blog.dto.ArticleDto;
import com.system.blogAdmin.blog.service.BlogArticleService;
import com.system.blogAdmin.bundary.service.MainIndexService;


import com.system.blogAdmin.utils.DtoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 首页
 * 作者： cnc
 * 创建时间：2017-07-05.
 * 版本：1.0
 */
@Controller
public class MainIndexController {

    @Autowired
    UserService userService;
    @Autowired
    BlogArticleService blogArticleService;
    @Autowired
    AuthResourceService authResourceService;
    @Autowired
    MainIndexService mainIndexService;


    @RequestMapping(value = {"","/","/index"},method = RequestMethod.GET)
    public String index(Model model){
       // UserDto userDto = DtoUtils.userToUserDto(userService.getById(new Long(1)));   //返回博主信息
       // model.addAttribute("userInfo", userDto);
       // model.addAttribute("titleInfo",mainIndexService.getMaxInfo());
        return "blogUser/index/index";
    }


    @RequestMapping(value = "/categoryList", method = RequestMethod.GET,produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String getIndex(){
        String str = "[";
        List<AuthResource> authList = authResourceService.listAuthResourceByPId(new Long(6));
        for (AuthResource authResource : authList) {
            str = str +"{name:'"+  authResource.getName() + "'},";
        }
        str = str.substring(0,str.length()-1) + "]";
        return str;
    }

    @RequestMapping(value = "/articleList", method = RequestMethod.GET,produces = "application/json;charset=utf-8;")
    @ResponseBody
    public EzPage<ArticleDto> getArticleList (EzPageParam pageParam){
        Map<String , Object> map  = new HashMap<String, Object>();
        map.put("recommend", 1); //查找出推荐文章
        return PageUtils.convertPageBean(DtoUtils.ArticleToBlog(blogArticleService.listPage(PageUtils.convert(pageParam), map)));
    }

    @RequestMapping(value = "/list/{contentType}", method = RequestMethod.POST,produces = "application/json;charset=utf-8;")
    @ResponseBody
    public EzPage<ArticleDto> blogArticleList1 (EzPageParam ezPageParam,@RequestParam(required = false) Long id, String title,String type,@PathVariable String contentType){
        Map<String , Object> map  = new HashMap<String, Object>();
        if(id!=null&&title.length()>0&&type.length()>0){
            if(type.equals("类别")){
                map.put("categoryId",id);
                map.put("contentType", contentType);
                map.put("contentType", contentType);
            }else if(id==null&&title.length()>0&&type.length()==0){
                map.put("content", title);
            }else{
                map.put("id", id);
            }
        }else{
            map.put("contentType", contentType);
        }
        return PageUtils.convertPageBean(DtoUtils.ArticleToBlog(blogArticleService.listPage(PageUtils.convert(ezPageParam),map)));
    }


}
